package main

import (
	"os"
	"sync"
)

// 发布日志

type PkgReleaseLog struct {
	mux sync.RWMutex
	fp  *os.File
}

func NewPkgReleaseLog(file string) (*PkgReleaseLog, error) {
	fp, err := openFile(file)
	if err != nil {
		return nil, err
	}
	return &PkgReleaseLog{
		fp: fp,
	}, nil
}

func (r *PkgReleaseLog) Add(p *PkgItem) error {
	r.mux.Lock()
	defer r.mux.Unlock()
	b := p.Json()
	_, err := r.fp.Write(b)
	_, _ = r.fp.Write([]byte{'\n'})
	return err
}

func (r *PkgReleaseLog) Close() error {
	r.mux.Lock()
	defer r.mux.Unlock()
	return r.fp.Close()
}
